/**
 * 
 */
package fpt.pits.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import fpt.pits.dto.CourseLanguageInfo;
import fpt.pits.util.Connection;

/**
 * @author yoyosakura
 *
 */
public class CourseLanguageInfoDAO {
    public List<CourseLanguageInfo> getByCourseID(int courseID) {
        List<CourseLanguageInfo> clList = null;
        CourseLanguageInfo clInfo = null;
        java.sql.Connection cn = null;
        java.sql.PreparedStatement ps = null;
        java.sql.ResultSet rs = null;
        String query = null;
        try {
            query = "SELECT cl.* " +
            		"FROM `course_language` cl " +
            		"WHERE cl.CourseID = ?";
            cn = Connection.getConnection();
            ps = cn.prepareStatement(query);
            ps.setInt(1, courseID);
            rs = ps.executeQuery();
            clList = new ArrayList<CourseLanguageInfo>();
            while (rs.next()) {
                if (rs.getByte("IsDeleted") == 0) {
                    clInfo = new CourseLanguageInfo();
                    clInfo.setCourseID(rs.getInt("CourseID"));
                    clInfo.setLangID(rs.getInt("LangID"));
                    clInfo.setIsDeleted(rs.getByte("IsDeleted"));
                    clList.add(clInfo);
                } else {
                    return null;
                }
            }
        } catch (Exception e) {
            System.err.println("Error: Get data from database " + e.getMessage());
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (ps != null) {
                    ps.close();
                }
                if (cn != null) {
                    cn.close();
                }
            } catch (SQLException sqle) {
                System.out.println("Error: No close connection " + sqle.getMessage());
            }
        }
        if (clList.isEmpty()) {
            return null;
        } else {
            return clList;
        }
    }
    public List<CourseLanguageInfo> getByLangID(int langID) {
        List<CourseLanguageInfo> clList = null;
        CourseLanguageInfo clInfo = null;
        java.sql.Connection cn = null;
        java.sql.PreparedStatement ps = null;
        java.sql.ResultSet rs = null;
        String query = null;
        try {
            query = "SELECT cl.* " +
                    "FROM `course_language` cl " +
                    "WHERE cl.CourseID = ?";
            cn = Connection.getConnection();
            ps = cn.prepareStatement(query);
            ps.setInt(1, langID);
            rs = ps.executeQuery();
            clList = new ArrayList<CourseLanguageInfo>();
            while (rs.next()) {
                if (rs.getByte("IsDeleted") == 0) {
                    clInfo = new CourseLanguageInfo();
                    clInfo.setCourseID(rs.getInt("CourseID"));
                    clInfo.setLangID(rs.getInt("LangID"));
                    clInfo.setIsDeleted(rs.getByte("IsDeleted"));
                    clList.add(clInfo);
                } else {
                    return null;
                }
            }
        } catch (Exception e) {
            System.err.println("Error: Get data from database " + e.getMessage());
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (ps != null) {
                    ps.close();
                }
                if (cn != null) {
                    cn.close();
                }
            } catch (SQLException sqle) {
                System.out.println("Error: No close connection " + sqle.getMessage());
            }
        }
        if (clList.isEmpty()) {
            return null;
        } else {
            return clList;
        }
    }
}
